﻿using System;
using System.Threading.Tasks;

using Linq2Crm.ObjectModel.DomainObject;

namespace Linq2Crm.Request
{
	public class InsertRequest : RequestBase
	{
		protected InsertRequest(Type type) : base(type)
		{
		}

		internal InsertRequest(Type type, IDomainObject entity)
			: this(type)
		{
			Entity = entity;
		}

		public IDomainObject Entity { get; set; }

		public Action<InsertResponse> Callback { get; set; }

		public static InsertRequest Create<T>(IDomainObject entity)
			where T : IDomainObject
		{
         if (entity == null)
         {
            throw new ArgumentNullException();
         }

			return new InsertRequest(typeof (T))
			       	{
			       		Entity = entity
			       	};
		}

		internal override void Execute()
		{
			DomainObjectContext.Instance.ExecuteInsertRequest(this);
		}

		internal override async Task<ResponseBase> ExecuteAsync()
		{
			var response = await DomainObjectContext.Instance.ExecuteInsertRequestAsync(this);
			return response;
		}
	}
}